package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.dto.CollectionYearDto;
import com.ant.storage.dto.ProductDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.CollectionYearMapper;
import com.ant.storage.mapper.DosageMapper;
import com.ant.storage.mapper.EnvironmentalMapper;
import com.ant.storage.pojo.CollectionYear;
import com.ant.storage.pojo.Dosage;
import com.ant.storage.pojo.Environmental;
import com.ant.storage.service.ICollectionYearService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-25
 */
@Service
public class CollectionYearServiceImpl extends ServiceImpl<CollectionYearMapper, CollectionYear> implements ICollectionYearService {

    @Autowired
    EnvironmentalMapper environmentalMapper;

    @Autowired
    DosageMapper dosageMapper;

    @Override
    public PageInfo<CollectionYear> listPage(ProductDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<CollectionYear> wrapper = new QueryWrapper<>();
            if (dto.getCompanyId() != null) {
                wrapper.eq("company_id", dto.getCompanyId());
            }
            if (dto.getIndustry()!=null) {
                wrapper.eq("industry", dto.getIndustry());
            }
            if (dto.getIndustryName()!=null) {
                wrapper.eq("industry_name", dto.getIndustryName());
            }
            if (dto.getIndustryCode()!=null) {
                wrapper.eq("industry_code", dto.getIndustryCode());
            }
            if (dto.getProductName()!=null) {
                wrapper.eq("product_name", dto.getIndustryName());
            }

            wrapper.orderByDesc("id");
            List<CollectionYear> list = this.baseMapper.selectList(wrapper);
            list.stream().forEach(item -> {
                List<Environmental> tal = environmentalMapper.selectList(new QueryWrapper<Environmental>()
                        .eq("product_id", item.getId()));
                item.setTalList(tal);
                tal.stream().forEach(t -> {
                    List<Dosage> dosageList = dosageMapper.selectList(new QueryWrapper<Dosage>()
                            .eq("parent_id", t.getId()));
                    t.setDosageList(dosageList);
                });
            });
            PageInfo<CollectionYear> info = new PageInfo<>(list);
            return info;
        } catch (Exception e) {
            System.out.println(e);
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public List<CollectionYear> listById(Integer id) {
        return baseMapper.selectList(new QueryWrapper<CollectionYear>().eq("company_id", id));
    }

    @Override
    public CollectionYear saveCollectionYear(CollectionYearDto dto) {
        CollectionYear year = new CollectionYear();
        String day = dto.getDay();
        List<String> list = getYear(day);
        if (list == null) {
            throw new AntException("日期格式错误");
        }
        year.setStartYears(list.get(0));
        year.setStartMonths(list.get(1));
        year.setEndYears(list.get(2));
        year.setEndMonths(list.get(3));
        year.setIndustry(dto.getIndustry());
        year.setCompanyId(dto.getCompanyId());
        year.setIndustryName(dto.getIndustryName());
        year.setIndustryCode(dto.getIndustryCode());
        year.setProductName(dto.getProductName());
        year.setProductPrice(dto.getProductPrice());
        year.setCreationTime(new Date());
        baseMapper.insert(year);
        return year;
    }

    private static List<String> getYear(String str) {
        List<String> list = new ArrayList<>();
        String regex = "(\\d+)\\.(\\d+)-(\\d+)\\.(\\d+)";

        // 编译正则并匹配
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(str);

        if (matcher.find()) {
            String startYear = matcher.group(1);  // 2024
            String startMonth = matcher.group(2); // 01
            String endYear = matcher.group(3);    // 2024
            String endMonth = matcher.group(4);   // 12

            list.add(startYear);
            list.add(startMonth);
            list.add(endYear);
            list.add(endMonth);

            // 输出结果
            System.out.println(startYear);  // 2024
            System.out.println(startMonth); // 01
            System.out.println(endYear);    // 2024
            System.out.println(endMonth);   // 12
            return list;
        } else {
            System.out.println("未匹配到符合格式的日期");
            return null;
        }
    }
}
